/*
 * @(#)GetFolderAndContentsRunner.java
 * Date 2013-09-08
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.query.es;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.ihecommon.service.ElasticSearchSource;

import us.hornerscorners.lamppost.model.ChainFilter;

import us.hornerscorners.lamppost.model.common.PersistableModelType;

import us.hornerscorners.lamppost.model.document.Association;

import us.hornerscorners.lamppost.registry.constant.AssociationType;
import us.hornerscorners.lamppost.registry.constant.QueryField;
import us.hornerscorners.lamppost.registry.constant.QuerySlotName;

import us.hornerscorners.lamppost.registry.model.QueryRequest;
import us.hornerscorners.lamppost.registry.model.QueryResponse;
import us.hornerscorners.lamppost.registry.model.RegistryObjects;

import us.hornerscorners.lamppost.registry.query.AssociationSourceModelFilter;
import us.hornerscorners.lamppost.registry.query.AssociationTargetModelFilter;
import us.hornerscorners.lamppost.registry.query.AssociationTargetURNFilter;
import us.hornerscorners.lamppost.registry.query.AssociationTypeFilter;

import us.hornerscorners.lamppost.registry.transformer.model.CodedElementTransformer;
import us.hornerscorners.lamppost.registry.transformer.model.PatientIdentifierTransformer;

import us.hornerscorners.lamppost.registry.utils.RegistryObjectsUtils;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-08
 * @author         Jim Horner
 */
public class GetFolderAndContentsRunner extends AbstractQueryRunner {

    /**
     * Constructs ...
     *
     *
     * @param config
     * @param elasticSearchSource
     * @param codedElementTransformer
     * @param patientIdentifierTransformer
     */
    public GetFolderAndContentsRunner(
            IHEConfig config, ElasticSearchSource elasticSearchSource,
            CodedElementTransformer codedElementTransformer,
            PatientIdentifierTransformer patientIdentifierTransformer) {

        super(config, elasticSearchSource, codedElementTransformer,
              patientIdentifierTransformer);

    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     */
    @Override
    public Map<PersistableModelType, QueryBuilder> createQuery(QueryRequest request) {

        Map<PersistableModelType, QueryBuilder> result = new HashMap<>();

        // Document Entries
        BoolQueryBuilder docEntryQuery = QueryBuilders.boolQuery();

        result.put(PersistableModelType.DocumentEntry, docEntryQuery);

        // confidentiality code
        if (request.hasValues(QuerySlotName.XDSDocumentEntryConfidentialityCode)) {

            docEntryQuery.must(createCodedElementQuery(request,
                    QuerySlotName.XDSDocumentEntryConfidentialityCode,
                    QueryField.DocumentEntryConfidentialityCodesCode,
                    QueryField.DocumentEntryConfidentialityCodesScheme));
        }

        if (request.hasValues(QuerySlotName.XDSDocumentEntryConfidentialityCodeScheme)) {

            docEntryQuery.must(createStringQuery(request,
                    QuerySlotName.XDSDocumentEntryConfidentialityCodeScheme,
                    QueryField.DocumentEntryConfidentialityCodesScheme));
        }

        // format code
        if (request.hasValues(QuerySlotName.XDSDocumentEntryFormatCode)) {

            docEntryQuery.must(createCodedElementQuery(request,
                    QuerySlotName.XDSDocumentEntryFormatCode,
                    QueryField.DocumentEntryFormatCodeCode,
                    QueryField.DocumentEntryFormatCodeScheme));
        }

        if (request.hasValues(QuerySlotName.XDSDocumentEntryFormatCodeScheme)) {

            docEntryQuery.must(createStringQuery(request,
                    QuerySlotName.XDSDocumentEntryFormatCodeScheme,
                    QueryField.DocumentEntryFormatCodeScheme));
        }

        BoolQueryBuilder folderQuery = QueryBuilders.boolQuery();

        result.put(PersistableModelType.Folder, folderQuery);

        if (request.hasValues(QuerySlotName.XDSFolderUniqueId)) {

            docEntryQuery.must(createUniqueIdentifierQuery(request,
                    QuerySlotName.XDSFolderUniqueId,
                    QueryField.DocumentEntryFoldersSourceUniqueIdRoot,
                    QueryField.DocumentEntryFoldersSourceUniqueIdExtension));

            folderQuery.must(createUniqueIdentifierQuery(request,
                    QuerySlotName.XDSFolderUniqueId, QueryField.FolderUniqueIdRoot,
                    QueryField.FolderUniqueIdExtension));
        }

        if (request.hasValues(QuerySlotName.XDSFolderEntryUUID)) {

            docEntryQuery.must(createStringQuery(request,
                    QuerySlotName.XDSFolderEntryUUID,
                    QueryField.DocumentEntryFoldersSourceEntryURN));

            folderQuery.must(createEntryURNQuery(request, PersistableModelType.Folder,
                    QuerySlotName.XDSFolderEntryUUID));
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     *
     * @throws ServiceActionException
     */
    @Override
    public QueryResponse run(QueryRequest request) throws ServiceActionException {

        QueryResponse result = super.run(request);

        RegistryObjects rol = result.getRegistryObjects();

        ChainFilter<Association> filter = new ChainFilter<>();

        filter.add(new AssociationTargetURNFilter(rol.getDocumentEntryURNs()));
        filter.add(new AssociationTypeFilter(AssociationType.HasMember));
        filter.add(new AssociationTargetModelFilter(PersistableModelType.DocumentEntry));
        filter.add(new AssociationSourceModelFilter(PersistableModelType.Folder));

        rol.addAllAssociations(RegistryObjectsUtils.pullAllAssociations(rol, filter));

        return result;
    }
}
